package com.lhn.bbs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhn.bbs.dto.ActionResult;
import com.lhn.bbs.dto.UserVo;
import com.lhn.bbs.entity.User;
import com.lhn.bbs.mapper.ReplyDao;
import com.lhn.bbs.mapper.ThemeDao;
import com.lhn.bbs.mapper.UserDao;
import com.lhn.bbs.service.UserService;
import com.lhn.bbs.vo.EditUser;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Author :lihainan
 * @ ClassNmae :UserServiceImpl
 * @ Description :TODO
 * @ Date :2021/3/6
 * @ Version 1.0
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService{

    @Autowired(required = false)
    private UserDao userDao ;

    @Autowired(required = false)
    private ThemeDao themeDao ;

    @Autowired(required = false)
    private ReplyDao replyDao ;

    /**
     * 用户登录
     * @param tel
     * @return
     */
    @Override
    public User findByTel(String tel) {
        return null;
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @Override
    public Boolean addUser(UserVo user) {
        long insert = userDao.addUser(user);
        if(insert == 1 ){
           return true ;
        }
        return false ;
    }

    @Override
    public Boolean backpassword(String password, String email) {
        return null;
    }

    /**
     * 验证学号是否被注册
     * @param sno
     * @return
     */
    @Override
    public User findBySno(String sno) {
        return userDao.findBySno(sno);
    }

    /**
     * 用户信息管理查询所有用户信息
     * @return
     */
    @Override
    public ActionResult findUserAll(int page ,int limit,int status) {
        PageHelper.startPage(page,limit);
        List<User> users  = userDao.selectUserall(status);
        for(User user1 : users){
            int themenum =  themeDao.findByIdcount(user1.getUserid());
            user1.setArticlenum(themenum);
            int replynum = replyDao.findReplyCountByUserid(user1.getUserid());
            user1.setReplynum(replynum);
            userDao.editnum(themenum,replynum,user1.getUserid());
        }
        PageInfo<User> pageInfo=new PageInfo(users);
        long total = pageInfo.getTotal();
        log.info("-------"+pageInfo);
        if(pageInfo != null ){
            return new ActionResult(200,"查询成功",pageInfo,total);
        }

        return new ActionResult(201,"查询失败",null,null);
    }

    /**
     * 根据用户账号删除用户
     * @param sno 账号
     * @return
     */
    @Override
    public ActionResult deleteuserBysno(String sno) {

       long lon = userDao.deleteBysno(sno);
       if(lon>0){
           return new ActionResult(200,"删除成功",null,null);
       }


        return new ActionResult(201,"删除失败",null,null);
    }

    @Override
    public ActionResult editUserBysno(EditUser editUser) {

        if(editUser != null) {
            long lon = userDao.updateUserBysno(editUser);
            if(lon >0 ){
                return new ActionResult(200,"修改成功",null,null);
            }
        }
        return new ActionResult(201,"修改失败",null,null);
    }


    @Override
    public ActionResult editpassword(String sno, String password) {
        if(password != null) {
            long lon = userDao.editpasswordBySno(sno,password);
            if(lon >0 ){
                return new ActionResult(200,"修改成功",null,null);
            }
        }
        return new ActionResult(201,"修改失败",null,null);
    }

    @Override
    public ActionResult editUserByid(String sno, String username, String sex, String email, String sdept, String sign) {
        if(sno != null) {
            long lon = userDao.editUserByid(sno,username,sex,email,sdept,sign);
            if(lon >0 ){
                return new ActionResult(200,"修改成功",null,null);
            }
        }
        return new ActionResult(201,"修改失败",null,null);
    }
}
